package com.raising.modules.sys.controller;

import com.raising.StaticParam;
import com.raising.framework.controller.BaseController;
import com.raising.framework.entity.ResultVo;
import com.raising.modules.sys.entity.Resource;
import com.raising.modules.sys.entity.Role;
import com.raising.modules.sys.entity.User;
import com.raising.modules.sys.service.ResourceService;
import com.raising.modules.sys.service.RoleService;
import com.raising.modules.sys.service.UserService;
import com.raising.modules.sys.utils.SysConfigUtil;
import com.raising.utils.JSystemUtils;
import com.raising.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 首页控制器
 * @author gaoy
 */
@Controller
@RequestMapping(value = "${adminPath}")
public class AdminIndexController extends BaseController {

	@Autowired
	private ResourceService resourceService;
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;

	/**
	 * 跳转到后台框架主页
	 * @author gaoy
	 * 2017 2017-5-9 上午10:20:41
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String index(Model model) throws Exception{
		List<Resource> menus_result = (List<Resource>) resourceService.getMyMenusByUserId().getData();
		model.addAttribute("menus",menus_result);

		//默认页面展示路径
		List<String> roleList = UserUtils.getLoginUser().getRoleIds();
		String firstRole = roleList.get(0);
		//根据角色查询默认首页路径
		Role role = roleService.get(firstRole);
		String indexUrl = role.getIndexUrl();
		model.addAttribute("indexUrl", indexUrl);
		model.addAttribute("isOpen","1");
		if(!"admin".equals(role.getSiteUrl())){
			return "redirect:"+role.getSiteUrl();
		}
		if(StaticParam.YES_STRING.equals(SysConfigUtil.getConfigValueByKey(StaticParam.CONFIG_ALLOW_MULTIPLE_MENUS,StaticParam.NO_STRING))){
			//可以有多个菜单窗口存在
			return "home";
		}
		//默认方式，只能有一个菜单窗口存在
		return "home_v1";
	}
	/**
	 * 系统管理平台主页
	 * @author GaoYuan
	 * @datetime 2017/10/18 下午4:24
	 */
	@RequiresPermissions("sys:index:welcome")
	@RequestMapping(value = "/welcome", method = RequestMethod.GET)
	public String welcome(Model model) throws Exception {
//		return "redirect:userCenter";
		//必须角色首页访问路径为welcome的才可以访问
		//默认页面展示路径
		List<String> roleList = UserUtils.getLoginUser().getRoleIds();
		String firstRole = roleList.get(0);
		//根据角色查询默认首页路径
		Role role = roleService.get(firstRole);
		String indexUrl = role.getIndexUrl();
		if(!"welcome".equals(indexUrl)){
			throw new Exception("没有权限访问");
		}
		//系统相关信息
		model.addAttribute("systemInfo", JSystemUtils.getSystemInfo());
		return "welcome";
	}

	/**
	 * 系统管理平台【userCenter】
	 * @author GaoYuan
	 * @datetime 2017/10/18 下午4:24
	 */
	@RequiresPermissions("sys:index:userCenter")
	@RequestMapping(value = "/userCenter", method = RequestMethod.GET)
	public String userCenter(Model model) throws Exception {
		return "userCenter";
	}


	/**
	 * 切换菜单收缩状态
	 * @author chenem
	 * @return
	 */
	@RequestMapping(value = "/changeNav.json",method = RequestMethod.POST)
	@ResponseBody
	public ResultVo changeNav(String isOpen){
		User user = UserUtils.getLoginUser();
		return ResultVo.success().desc("查询成功").data(userService.updateIsOpen(user.getId(),isOpen));
	}

}
